home *** CD-ROM | disk | FTP | other *** search
/ Utilities Professional 1-1500 / Utilities Professional 1-1500 (1994)(WPD)[!].iso / 12511500 / var1308.dms / var1308.adf / DNET2_10.LHA / DNet / Amiga / Sourcen.lha / client / cliterm.c next >
C/C++ Source or Header  |  1993-01-14  |  8KB  |  388 lines

  1.  
  2. /*
  3.  *  CLITERM.C
  4.  *
  5.  *  DNET (c)Copyright 1988-1989, Matthew Dillon, All Rights Reserved.
  6.  *
  7.  *  FTERM [-Nnet] [port]
  8.  */
  9.  
  10. #include "defs.h"
  11.  
  12. typedef struct IORequest RIO;
  13.  
  14.  
  15. #ifndef DEVICES_CONUNIT_H
  16. typedef struct ConUnit        CONUNIT;
  17. typedef struct IOStdReq     IOCON;
  18. #endif
  19.  
  20. short IgnoreNS = 0;
  21.  
  22. TA Ta = { (ubyte *)"topaz", 8 };
  23.  
  24. ITEXT IText[] = {
  25.     { 0, 1, JAM2, 0, 0, &Ta, (ubyte *)"Flush"          }
  26. };
  27.  
  28. ITEM Item[] = {
  29.     { NULL    , 0, 0, 100, 10, ITEMTEXT|COMMSEQ|ITEMENABLED|HIGHCOMP, 0, (APTR)&IText[0], NULL, 'o' }
  30. };
  31.  
  32. MENU Menu[] = {
  33.     { NULL    , 0, 0, 100, 20, MENUENABLED, "Control", &Item[0] }
  34. };
  35.  
  36. char Title[80];
  37.  
  38. NW Nw = {
  39.     0, 12, 640, 60, -1, -1,
  40.     NEWSIZE|CLOSEWINDOW|MENUPICK,
  41.     WINDOWSIZING|WINDOWDRAG|WINDOWDEPTH|WINDOWCLOSE|NOCAREREFRESH|ACTIVATE,
  42.     NULL, NULL, (ubyte *)Title, NULL, NULL,
  43.     32, 32, -1, -1, WBENCHSCREEN
  44. };
  45.  
  46. WIN *Win;
  47.  
  48. extern int Enable_Abort;
  49. char Buf[512];
  50. char Term[64] = { "FTERM (UNNAMED SHELL)" };
  51. char Cc;
  52.  
  53. struct IntuitionBase *IntuitionBase;
  54. struct GfxBase *GfxBase;
  55. IOCON *iocr, *iocw;
  56.  
  57. void OpenConsole ARGS((WIN *, IOCON **, IOCON **));
  58. void HandleIoctl ARGS((short, short, char, WIN *, IOCON *));
  59. void CloseConsole ARGS((IOCON *, IOCON *));
  60. void setsize ARGS((IOCON *, void *, WIN *));
  61. void localecho ARGS((int));
  62.  
  63. void main ARGS((int, char **));
  64.  
  65. int
  66. brk()
  67. {
  68.     return(0);
  69. }
  70.  
  71. void
  72. main(ac,av)
  73. char *av[];
  74. {
  75.     void *chan;
  76.     long imask, conmask, dmask, mask;
  77.     char notdone = 1;
  78.     char portspec = 0;
  79.     char *host = NULL;
  80.     uword port = PORT_AMIGASHELL;
  81.     int i;
  82.     int bp = 0;
  83.     char inputbuf[256];
  84.  
  85.     onbreak(brk);
  86.     sprintf(Title, "CLITerm V%s%s opening, wait..", VERSION, CLITERM_VERSION);
  87.     {
  88.     short i;
  89.     for (i = 1; i < ac; ++i) {
  90.         if (strncmp(av[i], "-N", 2) == 0) {
  91.         host = av[i] + 2;
  92.         continue;
  93.         }
  94.         portspec = 1;
  95.         port = atoi(av[i]);
  96.     }
  97.     }
  98.     if (portspec)
  99.     printf("Using port %ld\n", port);
  100. #ifndef LATTICE
  101.     Enable_Abort = 0;
  102. #endif
  103.     IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0);
  104.     GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0);
  105.     Win = OpenWindow(&Nw);
  106.     if (Win == NULL)
  107.     goto e1;
  108.     OpenConsole(Win, &iocr, &iocw);
  109.     if (iocr == NULL || iocw == NULL)
  110.     goto e3;
  111.  
  112.     /*
  113.      *    We delay here to allow DNET to go through its RESTART sequence
  114.      *    (when DNET automatically runs FTERM, it does so to quickly).
  115.      *    Such a hack!
  116.      */
  117.  
  118.     Delay(50 * 4);
  119.     chan = DOpen(host, port, 20, 15);
  120.  
  121.     if (!chan) {
  122.     puts("Unable to connect");
  123.     goto e3;
  124.     }
  125.     DQueue(chan, 32);
  126.     SetMenuStrip(Win, Menu);
  127.     SetWindowTitles(Win, Term, (char *)-1);
  128.     imask   = 1 << Win->UserPort->mp_SigBit;
  129.     dmask   = 1 << ((PORT *)chan)->mp_SigBit;
  130.     conmask = 1 << iocr->io_Message.mn_ReplyPort->mp_SigBit;
  131.     iocr->io_Data = (APTR)&Cc;
  132.     iocr->io_Length = 1;
  133.     SendIO((RIO *)iocr);
  134.  
  135.     setsize(iocw, chan, Win);
  136.  
  137.     while (notdone) {
  138.     mask = Wait(imask|dmask|conmask);
  139.     if (mask & imask) {
  140.         IMESS *im;
  141.         while (im = (IMESS *)GetMsg(Win->UserPort)) {
  142.         switch(im->Class) {
  143.         case NEWSIZE:
  144.             if (IgnoreNS) {
  145.             --IgnoreNS;
  146.             setsize(iocw, NULL, Win);
  147.             } else {
  148.             setsize(iocw, chan, Win);
  149.             }
  150.             break;
  151.         case CLOSEWINDOW:
  152.             notdone = 0;
  153.             break;
  154.         case MENUPICK:
  155.             switch((uword)((MENUNUM(im->Code)<<8)|ITEMNUM(im->Code))) {
  156.             case 0x0000:    /*    menu 0 item 0    */
  157.             DIoctl(chan, CIO_FLUSH, 0, 0);
  158.             break;
  159.             case 0x0001:    /*    menu 0 item 1    */
  160.             case 0x0002:    /*    menu 0 item 2    */
  161.             case 0x0100:    /*    menu 1 item 0    */
  162.             break;
  163.             }
  164.             break;
  165.         default:
  166.             break;
  167.         }
  168.         ReplyMsg((MSG *)im);
  169.         }
  170.     }
  171.     if (mask & dmask) {
  172.         char buf[256];
  173.         int n;
  174.         if ((n = DNRead(chan, buf, 256)) > 0) {
  175.         for (i = 0; i < n; i++) {
  176.             if (buf[i] == '\n') {
  177.             movmem(&buf[i], &buf[i + 1], n - i);
  178.             buf[i] = '\r';
  179.             i++;
  180.             n++;
  181.             }
  182.         }
  183.         iocw->io_Data = (APTR)buf;
  184.         iocw->io_Length = n;
  185.         DoIO((RIO *)iocw);
  186.         }
  187.         if (n == -2) {
  188.         short val;
  189.         short cmd;
  190.         char aux;
  191.  
  192.         cmd = DGetIoctl(chan, &val, &aux);
  193.         HandleIoctl(cmd, val, aux, Win, iocw);
  194.         /*
  195.         if (cmd == CIO_MODE) {
  196.             if (val)
  197.             SetWindowTitles(Win, "(Cooked)", (char *)-1);
  198.             else
  199.             SetWindowTitles(Win, "(Raw)", (char *)-1);
  200.         }
  201.         */
  202.         } else if (n < 0)
  203.         notdone = 0;
  204.     }
  205.     if (mask & conmask) {
  206.         if (CheckIO((RIO *)iocr)) {
  207.         WaitIO((RIO *)iocr);
  208.  
  209.         if (Cc == '\r')
  210.             Cc = '\n';
  211.         inputbuf[bp++] = Cc;
  212.  
  213.         if (Cc == 0x08) {
  214.             if (bp <= 1) {
  215.             bp = 0;
  216.             } else {
  217.             localecho(0x08);
  218.             localecho(0x20);
  219.             localecho(0x08);
  220.             bp -= 2;
  221.             }
  222.         } else if (Cc == 0x18) {
  223.             while (--bp > 0) {
  224.             localecho(0x08);
  225.             localecho(0x20);
  226.             localecho(0x08);
  227.             }
  228.         } else if (Cc == '\n') {
  229.             localecho('\n');
  230.             localecho('\r');
  231.             DWrite(chan, inputbuf, bp);
  232.             bp = 0;
  233.         } else {
  234.             localecho(Cc);
  235.         }
  236.  
  237.         iocr->io_Data = (APTR)&Cc;
  238.         iocr->io_Length = 1;
  239.         SendIO((RIO *)iocr);
  240. /*
  241.         if (Cc == '\r') {
  242.             Cc = '\n';
  243.             DWrite(chan, &Cc, 1);
  244.             iocr->io_Data = (APTR)&Cc;
  245.             iocr->io_Length = 1;
  246.             SendIO(iocr);
  247.         }
  248. */
  249.         }
  250.     }
  251.     }
  252.     AbortIO((RIO *)iocr);
  253.     WaitIO((RIO *)iocr);
  254.     SetWindowTitles(Win, "Closing...", (char *)-1);
  255.     DClose(chan);
  256. e3: CloseConsole(iocr,iocw);
  257.     CloseWindow(Win);
  258. e1: CloseLibrary((LIB *)IntuitionBase);
  259.     CloseLibrary((LIB *)GfxBase);
  260. }
  261.  
  262. void
  263. localecho(c)
  264. int c;
  265. {
  266.     char cc;
  267.  
  268.     cc = c;
  269.     iocw->io_Data = (APTR)&cc;
  270.     iocw->io_Length = 1;
  271.     DoIO((RIO *)iocw);
  272. }
  273.  
  274. void
  275. OpenConsole(win, piocr, piocw)
  276. IOCON **piocr, **piocw;
  277. WIN *win;
  278. {
  279.     PORT *port;
  280.     static IOCON iocr, iocw;
  281.     int error;
  282.  
  283.     port = CreatePort(NULL, 0);
  284.     iocr.io_Command = CMD_READ;
  285.     iocr.io_Data = (APTR)win;
  286.     iocr.io_Message.mn_Node.ln_Type = NT_MESSAGE;
  287.     iocr.io_Message.mn_ReplyPort = port;
  288.     error = OpenDevice("console.device", 0, (RIO *)&iocr, 0);
  289.     if (!error) {
  290.     iocw = iocr;
  291.     iocw.io_Command = CMD_WRITE;
  292.     *piocr = &iocr;
  293.     *piocw = &iocw;
  294.     } else {
  295.     *piocr = *piocw = NULL;
  296.     }
  297. }
  298.  
  299. void
  300. CloseConsole(iocr, iocw)
  301. IOCON *iocr;
  302. IOCON *iocw;
  303. {
  304.     IOCON *tmp = (iocr) ? iocr : iocw;
  305.     if (tmp) {
  306.     CloseDevice((RIO *)tmp);
  307.     DeletePort(tmp->io_Message.mn_ReplyPort);
  308.     }
  309. }
  310.  
  311. void
  312. setsize(iocw, chan, win)
  313. IOCON *iocw;
  314. void *chan;
  315. WIN *win;
  316. {
  317.     struct ConUnit *cu = (struct ConUnit *)iocw->io_Unit;
  318.     static char IStr[] = { "\033c\23320l\233t\233u" };
  319.  
  320.     /*
  321.     if (Cooked & 4)
  322.     IStr[5] = 'h';
  323.     else
  324.     IStr[5] = 'l';
  325.     */
  326.     iocw->io_Data = (APTR)IStr;
  327.     iocw->io_Length = sizeof(IStr) - 1;
  328.     DoIO((RIO *)iocw);
  329.     if (chan) {
  330.     DIoctl(chan, CIO_SETROWS, (uword)(cu->cu_YMax+1), 0);
  331.     DIoctl(chan, CIO_SETCOLS, (uword)(cu->cu_XMax+1), 0);
  332.     }
  333.     sprintf(Term, "FTERM   %ld x %ld", cu->cu_YMax+1, cu->cu_XMax+1);
  334.     SetWindowTitles(win, Term, (char *)-1);
  335. }
  336.  
  337. void
  338. HandleIoctl(cmd, val, aux, win, iocw)
  339. short cmd, val;
  340. char aux;
  341. WIN *win;
  342. IOCON *iocw;
  343. {
  344.     static short saverows;
  345.     short height, width;
  346.     short dx, dy;
  347.  
  348.     switch(cmd) {
  349.     case CIO_MODE:
  350.     /*
  351.     Cooked = val;
  352.     */
  353.     break;
  354.     case CIO_SETROWS:
  355.     saverows = val;
  356.     break;
  357.     case CIO_SETCOLS:
  358.     width = val * win->RPort->TxWidth + win->BorderLeft + win->BorderRight;
  359.     height= saverows * win->RPort->TxHeight + win->BorderTop + win->BorderBottom;
  360.  
  361.     dx = win->WScreen->Width - (win->LeftEdge + width);
  362.     if (dx > 0)
  363.         dx = 0;
  364.     if (-dx > win->LeftEdge) {
  365.         dx = -win->LeftEdge;
  366.         width = win->WScreen->Width;
  367.     }
  368.  
  369.     dy = win->WScreen->Height - (win->TopEdge + height);
  370.     if (dy > 0)
  371.         dy = 0;
  372.     if (-dy > win->TopEdge) {
  373.         dy = -win->TopEdge;
  374.         height = win->WScreen->Height;
  375.     }
  376.  
  377.     if (dx || dy) {
  378.         MoveWindow(win, dx, dy);
  379.     }
  380.     if (win->Width != width || win->Height != height) {
  381.         SizeWindow(win, width - win->Width, height - win->Height);
  382.         ++IgnoreNS;
  383.     }
  384.     break;
  385.     }
  386. }
  387.  
  388.